package com.project.server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.server.common.Result;
import com.project.server.model.DiningTable;
import com.project.server.service.DiningTableService;
import com.project.server.vo.DiningTableVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "餐桌管理")
@RestController
@RequestMapping("/api/dining/table")
@RequiredArgsConstructor
public class DiningTableController {

    private final DiningTableService diningTableService;

    @Operation(summary = "分页查询餐桌列表")
    @GetMapping("/page")
    public Result<Page<DiningTableVO>> page(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String tableNumber,
            @RequestParam(required = false) Long restaurantId,
            @RequestParam(required = false) Integer status) {
        return Result.success(diningTableService.page(page, pageSize, tableNumber, restaurantId, status));
    }

    @Operation(summary = "获取餐桌详情")
    @GetMapping("/{id}")
    public Result<DiningTable> getById(@PathVariable Long id) {
        return Result.success(diningTableService.getById(id));
    }

    @Operation(summary = "新增餐桌")
    @PostMapping
    public Result<Void> save(@RequestBody @Validated DiningTable diningTable) {
        diningTableService.save(diningTable);
        return Result.success();
    }

    @Operation(summary = "更新餐桌")
    @PutMapping
    public Result<Void> update(@RequestBody @Validated DiningTable diningTable) {
        diningTableService.update(diningTable);
        return Result.success();
    }

    @Operation(summary = "删除餐桌")
    @DeleteMapping("/{id}")
    public Result<Void> remove(@PathVariable Long id) {
        diningTableService.removeById(id);
        return Result.success();
    }

    @Operation(summary = "更新餐桌状态")
    @PutMapping("/status/{id}/{status}")
    public Result<Void> updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        diningTableService.updateStatus(id, status);
        return Result.success();
    }

    @Operation(summary = "获取餐厅的空闲餐桌")
    @GetMapping("/available/{restaurantId}")
    public Result<List<DiningTable>> getAvailableTables(@PathVariable Long restaurantId) {
        return Result.success(diningTableService.getAvailableTables(restaurantId));
    }
}
